<?php
namespace app\index\model;
use think\Model;
class Collect extends Model
{
    /**
     * 根据条件(数组)查询收藏表,如果有数据返回1 反之返回0
     * @param $data
     * @return int
     */
    public function queryCollect ($data)
    {
        $info = $this->where($data)->find();
        if(empty($info)){
            return 0;
        }else{
            return 1;
        }
    }

    /**
     * 向收藏表中添加数据,添加成功,返回自增id 失败,返回0
     * @param $data
     * @return int|mixed
     */
    public function addCollect ($data)
    {
        $result = $this->save($data);
        if($result > 0){
            return $this->id;
        }else{
            return 0;
        }
    }

    /**
     * 向收藏表中删除数据,删除成功,返回自增id 失败,返回0
     * @param $data
     * @return int|mixed
     */
    public function deleteCollect ($data)
    {
        $result = $this->where($data)->delete();
        if($result > 0){
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 通过用户id查询他的所有收藏的帖子
     * @param $user_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function queryCollectByUserId ($user_id)
    {
        $list = $this->alias("coll")
            ->join("fly_card c","coll.card_id = c.id")
            ->field(["coll.*","c.name"])
            ->where("coll.user_id=$user_id")
            ->order("coll.create_time")->paginate(10);
//            ->select();
        if(empty($list[0])){
            $arr['data']=[];
            $arr['page']=[];
            return $arr;
        }else{
            foreach($list as $key=>$val){
                $data[$key] = $val->data;
            }
            $arr['data']=$data;
            $arr['page']=$list->render();
            return $arr;
        }
    }
}

?>